package com.masterclient.app.businessmodule.minemodule.redpack.detail;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.modules.bridge.BridgeActivity;
import com.masterclient.app.businessmodule.minemodule.redpack.adapter.RedPackedProductAdapter;
import com.masterclient.app.modules.coupon.beans.RedPackedDetailsBean;
import com.masterclient.app.businessmodule.minemodule.redpack.contract.RedPackedDetailContract;
import com.masterclient.app.businessmodule.minemodule.redpack.contract.RedPackedDetailPresenter;
import com.masterclient.app.modules.income.model.Profit;
import com.masterclient.app.businessmodule.minemodule.point.model.PointExchange;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.Utils;
import com.masterclient.app.widget.PriceIntegralLayout;

import java.util.List;
import butterknife.BindView;

/**
 * 作者：sean
 * 创建时间：2017/8/7 15:09
 * 功能说明：
 */

public class RedPackedDetailActivity extends FCBusinessActivity<RedPackedDetailPresenter> implements RedPackedDetailContract.View {

    @BindView(R.id.tv_red_pack_pay)
    TextView tvRedPackPay;
    @BindView(R.id.ll_container)
    LinearLayout llContainer;
    @BindView(R.id.tv_all_price)
    TextView tvAllPrice;
    @BindView(R.id.tv_freight)
    TextView tvFreight;
    @BindView(R.id.tv_discount)
    TextView tvDiscount;
    @BindView(R.id.tv_final_price)
    TextView tvFinalPrice;
    @BindView(R.id.ll_discount)
    LinearLayout llDiscount;
    @BindView(R.id.ll_repack)
    LinearLayout llRepack;
    @BindView(R.id.tv_repack)
    TextView tvRepack;
    @BindView(R.id.ll_return)
    LinearLayout llReturn;
    @BindView(R.id.ll_prices)
    LinearLayout llPrices;
    @BindView(R.id.ll_con)
    LinearLayout llCon;
    @BindView(R.id.rl_con)
    RelativeLayout rlCon;
    @BindView(R.id.tv_tag)
    TextView tvTag;

    private List<String> product_id;
    private String type;

    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_redpacked_detail;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        type = getBundleString("title_type");
        tvTag.setText("0".equals(type) ? "获取数量（币）" : "支出数量（币）");
        navigationBar.setTitle("0".equals(type) ? "收入详情" : "支出详情");
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {

        mPresenter.getRedPackedOrderDetail(getBundleString("id"),0);
    }

    @Override
    public void getDetailSuccess(RedPackedDetailsBean dataBean) {
        if (dataBean == null) return;
        tvRedPackPay.setText(("0".equals(type) ? "+" : "-") + dataBean.getSh_amount());

        if (dataBean.getSh_tip() != null && dataBean.getSh_tip().size() > 0) {
            rlCon.setVisibility(View.VISIBLE);
            for (RedPackedDetailsBean.ShTipBean bean : dataBean.getSh_tip()) {
                View item = View.inflate(mContext, R.layout.item_cash_detail, null);
                ((TextView) item.findViewById(R.id.tv_tag)).setText(bean.getSh_title());
                TextView textView = (TextView) item.findViewById(R.id.tv_desc);
                textView.setText(bean.getSh_content());
                if (!TextUtils.isEmpty(bean.getSh_url())) {
                    textView.setTextColor(0xFF7399E5);
                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivitry(BridgeActivity.class, new String[][]{{"url", bean.getSh_url()}});
                        }
                    });
                }
                llCon.addView(item);
            }
        }

        llContainer.removeAllViews();

        if (dataBean.getSh_trade() != null && !TextUtils.isEmpty(dataBean.getSh_trade().getSh_subtotal())) {
            llPrices.setVisibility(View.VISIBLE);
            tvAllPrice.setText("¥" + dataBean.getSh_trade().getSh_product_price());
            tvFreight.setText("¥" + dataBean.getSh_trade().getSh_shipping_total());

            if (Float.valueOf(dataBean.getSh_trade().getSh_discount_total()) > 0) {
                tvDiscount.setText("-¥" + dataBean.getSh_trade().getSh_discount_total());
            } else {
                llDiscount.setVisibility(View.GONE);
            }
            if (Float.valueOf(dataBean.getSh_trade().getSh_bupiao_total()) > 0) {
                tvRepack.setText("-¥" + dataBean.getSh_trade().getSh_bupiao_total());
            } else {
                llRepack.setVisibility(View.GONE);
            }

            Utils.set_price_style(tvFinalPrice, "¥" + dataBean.getSh_trade().getSh_subtotal());

            for (RedPackedDetailsBean.ShTradeBean.ShOrdersBean ordersBean : dataBean.getSh_trade().getSh_orders()) {
                View view = View.inflate(mContext, R.layout.item_customer_order_shop, null);
                TextView tv = (TextView) view.findViewById(R.id.tv_shop_name);
                TextView tv_shop_tag = (TextView) view.findViewById(R.id.tv_shop_tag);
                view.findViewById(R.id.ll_layout).setVisibility(View.VISIBLE);
                view.findViewById(R.id.line).setVisibility(View.GONE);

                view.findViewById(R.id.order_no_ll).setVisibility(View.VISIBLE);
                ((TextView) view.findViewById(R.id.order_no_tv)).setText(ordersBean.getSh_order_no());

                tv.setText(ordersBean.getSh_seller().getSh_store_name());
                LinearLayout ll_shop_container = (LinearLayout) view.findViewById(R.id.ll_shop_container);
//                ll_shop_container.setOnClickListener(v->{
//                    Intent intent = new Intent(mContext, OrderDetailActivity.class);
//                    intent.putExtra("order_no",ordersBean.getSh_order_no());
//                    mContext.startActivity(intent);
//                });
                ll_shop_container.removeAllViews();
                int i = 0;
                for (RedPackedDetailsBean.ShTradeBean.ShOrdersBean.ShProductsBean productsBean : ordersBean.getSh_products()) {
                    View view_goods = View.inflate(mContext, R.layout.item_customer_order_shop_goods, null);
                    View v_divide = view_goods.findViewById(R.id.v_divide);
                    ImageView imageView = (ImageView) view_goods.findViewById(R.id.iv_good);
                    TextView tv_title = (TextView) view_goods.findViewById(R.id.tv_title);
                    TextView tv_sku = (TextView) view_goods.findViewById(R.id.tv_skus);
                    PriceIntegralLayout ll_price = (PriceIntegralLayout) view_goods.findViewById(R.id.ll_price);
//                    Utils.setDin(tv_price, mContext);
                    TextView tv_gos_num = (TextView) view_goods.findViewById(R.id.tv_gos_num);
                    TextView tv_after_sale = (TextView) view_goods.findViewById(R.id.tv_after_sale);
                    GlideUtil.displayRoundImage(mContext, productsBean.getSh_image(), imageView, 4, R.mipmap.defaults, 107, 107);
//                    App.picasso.load(productsBean.getSh_image())
//                            .transform(new NewPicassoRoundTransform(ScreenUtils.dp2px(4)))
//                            .resize(ScreenUtils.dp2px(107),ScreenUtils.dp2px(107))
//                            .error(R.mipmap.defaults).placeholder(R.mipmap.defaults).into(imageView);
                    tv_title.setText(productsBean.getSh_name());
                    View view_line = view_goods.findViewById(R.id.view_line);

                    view_line.setVisibility(i == ordersBean.getSh_products().size()-1?View.GONE:View.VISIBLE);
                    if (i == 0) {
                        v_divide.setVisibility(View.GONE);
                    } else {
                        v_divide.setVisibility(View.VISIBLE);
                    }

                    i++;

                    tv_after_sale.setVisibility(View.GONE);

                    String skus = "";
                    List<PointExchange.Property> sh_property = productsBean.getSh_property();
                    for (PointExchange.Property propertyBean : sh_property) {
                        skus += ("“" + propertyBean.getSh_alias_name() + "”");
                    }
                    tv_sku.setText("规格：" + skus);
                    //tv_price.setText("¥"+productsBean.getSh_price());
                    ll_price.setData((int) (Float.valueOf(productsBean.getSh_integral()) / Float.valueOf(productsBean.getSh_buy_qty()))+"",productsBean.getSh_price());

//                    if (productsBean.getSh_integral() != null && Float.valueOf(productsBean.getSh_integral()) > 0) {
//                        int temp = (int) (Float.valueOf(productsBean.getSh_integral()) / Float.valueOf(productsBean.getSh_buy_qty()));
//                        String content = temp + " 金币";
//                        SpannableString spannableString = new SpannableString(content);
//                        spannableString.setSpan(new RelativeSizeSpan(0.75f), content.length() - 3, content.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//                        tv_price.setText(spannableString);
////                        tv_price.setText(temp + "金币");
//                    } else {
//                        Utils.set_price_style(tv_price, "¥" + productsBean.getSh_price());
//                    }
                    tv_gos_num.setText("x" + productsBean.getSh_buy_qty());
                    ll_shop_container.addView(view_goods);
                }
                llContainer.addView(view);
            }


        }

        if (dataBean.getSh_order_product() != null && dataBean.getSh_order_product().size() > 0) {
            llReturn.setVisibility(View.VISIBLE);
            for (int i = 0; i < dataBean.getSh_order_product().size(); i++) {
                RedPackedDetailsBean.ShTradeBean.ShOrdersBean.ShProductsBean productsBean = dataBean.getSh_order_product().get(i);
                View view_goods = View.inflate(mContext, R.layout.item_customer_order_shop_goods, null);
                View v_divide = view_goods.findViewById(R.id.v_divide);
                ImageView imageView = (ImageView) view_goods.findViewById(R.id.iv_good);
                TextView tv_title = (TextView) view_goods.findViewById(R.id.tv_title);
                TextView tv_sku = (TextView) view_goods.findViewById(R.id.tv_skus);
                PriceIntegralLayout ll_price = (PriceIntegralLayout) view_goods.findViewById(R.id.ll_price);
//                Utils.setDin(tv_price, mContext);
                TextView tv_gos_num = (TextView) view_goods.findViewById(R.id.tv_gos_num);
                TextView tv_after_sale = (TextView) view_goods.findViewById(R.id.tv_after_sale);
                GlideUtil.displayRoundImage(mContext, productsBean.getSh_image(), imageView, 4, R.mipmap.defaults, 107, 107);
//                    App.picasso.load(productsBean.getSh_image())
//                            .transform(new NewPicassoRoundTransform(ScreenUtils.dp2px(4)))
//                            .resize(ScreenUtils.dp2px(107),ScreenUtils.dp2px(107))
//                            .error(R.mipmap.defaults).placeholder(R.mipmap.defaults).into(imageView);
                tv_title.setText(productsBean.getSh_name());
                View view_line = view_goods.findViewById(R.id.view_line);
                v_divide.setVisibility(i == 0 ? View.GONE : View.VISIBLE);
                    view_line.setVisibility(i == dataBean.getSh_order_product().size()-1?View.GONE:View.VISIBLE);
//                if(i == 0){
//                    v_divide.setVisibility(View.GONE);
//                }else {
//                    v_divide.setVisibility(View.VISIBLE);
//                }
//
//                i++;
                tv_after_sale.setVisibility(View.GONE);
                String skus = "";
                List<PointExchange.Property> sh_property = productsBean.getSh_property();
                for (PointExchange.Property propertyBean : sh_property) {
                    skus += ("“" + propertyBean.getSh_alias_name() + "”");
                }
                tv_sku.setText("规格：" + skus);
                //tv_price.setText("¥"+productsBean.getSh_price());
                ll_price.setData((int) (Float.valueOf(productsBean.getSh_integral()) / Float.valueOf(productsBean.getSh_buy_qty()))+"",productsBean.getSh_price());

//                if (productsBean.getSh_integral() != null && Float.valueOf(productsBean.getSh_integral()) > 0) {
//                    int temp = (int) (Float.valueOf(productsBean.getSh_integral()) / Float.valueOf(productsBean.getSh_buy_qty()));
//                    String content = temp + " 金币";
//                    SpannableString spannableString = new SpannableString(content);
//                    spannableString.setSpan(new RelativeSizeSpan(0.75f), content.length() - 3, content.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//                    tv_price.setText(spannableString);
////                        tv_price.setText(temp + "金币");
//                } else {
//                    Utils.set_price_style(tv_price, "¥" + productsBean.getSh_price());
//                }
                tv_gos_num.setText("x" + productsBean.getSh_buy_qty());
                llContainer.addView(view_goods);
            }

        }

        if (dataBean.getSh_profits() != null && dataBean.getSh_profits().size() > 0) {
            for (Profit item : dataBean.getSh_profits()) {
                View view = View.inflate(mContext, R.layout.income_commission_item, null);
                RecyclerView listview = view.findViewById(R.id.listview);
                RedPackedProductAdapter adapter = new RedPackedProductAdapter(1);

                LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
                listview.setLayoutManager(mLayoutManager);
                listview.setAdapter(adapter);
                adapter.setNewData(item.getSh_items());

                ImageView flag = view.findViewById(R.id.flag);
                flag.setImageResource("1".equals(item.getSh_profit_type()) ? R.mipmap.mai :
                        "2".equals(item.getSh_profit_type()) ? R.mipmap.tuiguang : R.mipmap.sheng);

                ImageView image = view.findViewById(R.id.image);
                GlideUtil.displayCircleImage(mContext, item.getSh_avatar(), image, R.mipmap.user_head_default_icon);

                ((TextView) view.findViewById(R.id.nick)).setText(item.getSh_show_name());
                ((TextView) view.findViewById(R.id.time)).setText(item.getSh_profit_time());
                view.findViewById(R.id.tv_price).setVisibility(View.INVISIBLE);
                view.findViewById(R.id.tv_price_title).setVisibility(View.INVISIBLE);
                view.findViewById(R.id.status).setVisibility(View.INVISIBLE);
                view.findViewById(R.id.tv_unit).setVisibility(View.INVISIBLE);
                llContainer.addView(view);
            }
        }

    }

    @Override
    public void onClickPageStateErrorView() {
        super.onClickPageStateErrorView();
        mPresenter.getRedPackedOrderDetail(getBundleString("id"),0);
    }

    @Override
    public void getDetailFailed() {

    }
}
